<?php
    session_start();
    require_once '../includes/runtime/lib/Propel.php';
    Propel::init("../includes/build/conf/easylib-conf.php");
    set_include_path("../includes/build/classes" . PATH_SEPARATOR . get_include_path());
    if(isset($_SESSION["admin_login"])&&isset($_SESSION["admin_key"])){
        $query = TabAdminQuery::create()->filterByLogin($_SESSION["admin_login"])->findOne();
        if($query!=null){
            if(md5($query->getPass())==$_SESSION["admin_key"]){
                switch ($_GET["action"]){
                    case "liste":
                        $stations = TabStationQuery::create()->find();
                        echo $stations->toJSON();
                        break;
                    case "modifier":
                        $station = TabStationQuery::create()->filterByIdStation($_GET["id"])->findOne();
                        $station->setStationName(stripslashes($_GET["nom"]));
                        $station->setStationAddress(stripslashes($_GET["adresse"]));
                        $station->setStationLatitude($_GET["latitude"]);
                        $station->setStationLongitude($_GET["longitude"]);
                        $station->save();
                        break;
                    case "ajouter":
                        $station = new TabStation();
                        $station->setStationName(stripslashes($_GET["nom"]));
                        $station->setStationAddress(stripslashes($_GET["adresse"]));
                        $station->setStationLatitude($_GET["latitude"]);
                        $station->setStationLongitude($_GET["longitude"]);
                        $station->save();
                        break;
                    case "liste_information":
                        $infostation = TabInformationStationQuery::create()->filterByFkStation($_GET["id"])->orderByLastUpdate()->find();
                        $results = array();
                        foreach ($infostation as $instation){
                            $instation instanceof TabInformationStation;
                            $date = $instation->getLastUpdate(null);
                            $results[] = array(
                                "AvailableBikes" => $instation->getAvailableBikes(),
                                "LastUpdate" => $date->getTimestamp()*1000
                            );
                        }
                        
                        echo json_encode($results);
                        break;
                }
            }
        }
    }
?>
